package com.example.androidxtest.lazy;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;

import java.util.ArrayList;


/**
 * 继承自新的adapter，FragmentStatePageAdapter
 * 构造方法，调用父类super方法时，添加传入 FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
 * 参考链接：http://events.jianshu.io/p/8898e287658d
 */
public class FPAdater extends FragmentStatePagerAdapter {

    ArrayList<Fragment> fragments;
    ArrayList<String> titles;
    public FPAdater(FragmentManager fm) {
        super(fm,FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
    }

    public FPAdater(FragmentManager fm, ArrayList<Fragment> fragments, ArrayList<String> titles) {
        super(fm,FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        this.fragments = fragments;
        this.titles = titles;
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }
    /*让TabLayout能获取到title*/
    @Override
    public CharSequence getPageTitle(int position) {
        return titles.get(position);
    }
}
